﻿using System;
using System.Collections.Generic;
using System.Data;
using DataBase;

namespace DBService
{
	public class DbRole : DbObject
	{
		public string Name
		{
			get
			{
				return this.name;
			}
			set
			{
				this.name = value;
			}
		}

		public string Group { get; set; }

		public string Description
		{
			get
			{
				return this.description;
			}
			set
			{
				this.description = value;
			}
		}

		public DbRole(int rID, string rName, string rDesc, IDatabase rdb)
		{
			this.mDatabase = rdb;
			this.mId = rID;
			this.name = rName;
			this.description = rDesc;
		}

		public DbRole(string rName, string rDesc, string rGroup)
		{
			this.name = rName;
			this.description = rDesc;
			this.Group = rGroup;
		}

		public List<DbRight> GetRights()
		{
			List<DbRight> result;
			try
			{
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				List<DbRight> list = new List<DbRight>();
				string sql = string.Format("SELECT tbright.Id,tbright.Name,tbright.Description FROM tbright INNER JOIN tbRoleRightRelation ON tbright.Id=tbRoleRightRelation.RightId where tbRoleRightRelation.RoleId={0}", this.mId);
				DataTable dataTable = this.mDatabase.ExecQuery(sql);
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				if (dataTable != null)
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						list.Add(new DbRight(Convert.ToInt32(dataTable.Rows[i]["Id"]), dataTable.Rows[i]["Name"].ToString(), dataTable.Rows[i]["Description"].ToString(), this.mDatabase));
					}
				}
				result = list;
			}
			catch (Exception ex)
			{
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public override void AddToDb(IDatabase db)
		{
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("insert into tbRole (Name,Description,RoleGroup) VALUES ('{0}','{1}','{2}')", this.name, this.description, this.Group);
				db.ExecuteCommand(sql);
				string sql2 = string.Format("select max(Id) from tbRole", Array.Empty<object>());
				DataTable dataTable = db.ExecQuery(sql2);
				this.mId = Convert.ToInt32(dataTable.Rows[0][0]);
				this.mDatabase = db;
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
		}

		public static List<DbRole> GetAllRoles(IDatabase db)
		{
			List<DbRole> result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				List<DbRole> list = new List<DbRole>();
				string sql = string.Format("select Id,Name,Description from tbRole", Array.Empty<object>());
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable != null)
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						list.Add(new DbRole(Convert.ToInt32(dataTable.Rows[i]["Id"]), dataTable.Rows[i]["Name"].ToString(), dataTable.Rows[i]["Description"].ToString(), db));
					}
				}
				result = list;
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public static List<DbRole> GetAllRoles(IDatabase db, string group)
		{
			List<DbRole> result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				List<DbRole> list = new List<DbRole>();
				string sql = string.Format("select Id,Name,Description from tbRole where RoleGroup='{0}'", group);
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable != null)
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						list.Add(new DbRole(Convert.ToInt32(dataTable.Rows[i]["Id"]), dataTable.Rows[i]["Name"].ToString(), dataTable.Rows[i]["Description"].ToString(), db));
					}
				}
				result = list;
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public override void RemoveFromDb()
		{
			try
			{
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				string sql = string.Format("DELETE FROM tbRole WHERE Id = {0}", this.mId);
				this.mDatabase.ExecuteCommand(sql);
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex;
			}
		}

		public void UpdateRights(List<DbRight> rightList)
		{
			try
			{
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				string sql = string.Format("DELETE FROM tbRoleRightRelation WHERE RoleId = {0}", this.mId);
				this.mDatabase.ExecuteCommand(sql);
				foreach (DbRight dbRight in rightList)
				{
					string sql2 = string.Format("insert into tbRoleRightRelation (RoleId, RightId) VALUES ({0},{1})", this.mId, dbRight.Id);
					this.mDatabase.ExecuteCommand(sql2);
				}
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex;
			}
		}

		public override void UpdateToDb()
		{
		}

		public override string ToString()
		{
			return this.name;
		}

		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		public override bool Equals(object obj)
		{
			return obj is DbRole && ((DbRole)obj).Id == this.mId;
		}

		private string name;

		private string description;
	}
}
